var clock_;

var clock_base;

var clock_timeLine;

var clock_ContentLine;



//组织需要提交的数据

var beSubInMDay;//在月中重复

var beSubInWDay;//星期重复

var beSubMonth;

var beSubYear;



function initClock()

{

	createClock();

	clock_createScheduleOnDayTable();

	return;

}



//初始化可拉动时间条的主体

function createClock()

{

	clock_base=$("<div></div>").css({'width':'250px', 'height':'410px', 'background':'grey' ,'position': 'absolute','overflow':'hidden'});

	clock_timeLine=$("<div></div>").css({'width':'40px', 'height':'400px', 'background':'red' ,'float': 'left','overflow':'hidden'});

	clock_timeLine.append(getArrange);

	clock_base.append(clock_timeLine);

	clock_ContentLine=$("<div></div>").css({'width':'210px', 'height':'400px', 'background':'green' ,'float': 'left','overflow':'hidden'});

	clock_base.append(clock_ContentLine);

	

	var clock_scaleInTimeLine=$("<table></table>").css({'width':'210px', 'height':'400px'});

	var clock_scaleInTimeLineTRtemp=$("<tr><td width='100%' height='2%'></td></tr>");

	clock_scaleInTimeLine.append(clock_scaleInTimeLineTRtemp);

	for(var i=0;i<24;i++)

	{

		var temp=$("<tr><td width='100%' height='3%'><font size='1'>"+i+"</font></td></tr>");

		clock_scaleInTimeLine.append(temp);

	}

	clock_scaleInTimeLine.append(clock_scaleInTimeLineTRtemp);

	clock_timeLine.append(clock_scaleInTimeLine);

	

	

	//添加一个关闭的小按钮

	var clock_baseClose=$("<div>取消</div>").css({'width':'100%', 'height':'10px','overflow':'hidden','float': 'left','cursor': 'pointer'});

	clock_base.append(clock_baseClose);

	clock_baseClose.click(function(){

		clock_displayClockOff();

	});

	

	return;

}



/**

 * 获得范围

 * @returns

 */

var clock_getArrange_check=0;

var clock_offsetTop=0;

var clock_divLong=0;

function getArrange()

{

	var Yover;

	var Yog;

	

	var yl;

	

	$(this).mousedown(function(e){

		if(clock_getArrange_check==0)

		{

			Yog=e.pageY;

		}		

	});

	

	$(this).mouseup(function(e){

		if(clock_getArrange_check==0)

		{

			Yover=e.pageY;

			if((Yover-Yog)>0)

			{

				yl=Yover-Yog;

			}

			else if((Yover-Yog)==0)

			{

				yl=0;

			}

			else

			{

				yl=Yog-Yover;

			}

			

			/**

			 * 防止重复时间被提交

			 */

			//是否与已有时间重复

			if(clock_checkContentLineDivForRepeat(Yog,Yover))

			{

				return;

			}

			

			

			var tmp=$("<div></div>").css({'width':$(this).width()+'px', 'height':yl+'px','top':(Yog-$(this).offset().top)+'px', 'background':'grey','overflow':'hidden','position': 'absolute','opacity':'0.5'});

			$(this).append(tmp);

			var tmpC=$("<div></div>").css({'width':clock_ContentLine.width()+'px', 'height':yl+'px','top':(Yog-$(this).offset().top)+'px', 'background':'grey','overflow':'hidden','position': 'absolute','opacity':'0.5','cursor': 'pointer'});

			clock_ContentLine.append(tmpC);

			

			

			//计算被框中的时间的区域

			tmpC.empty();

			tmpC.append(clock_getMinAndHour((Yog-$(this).offset().top),$(this).height(),yl));

			tmpC.click(function(){

				

				//被选择的时间

				clock_scheduleOnDayTableInsertTime.empty();

				clock_scheduleOnDayTableInsertTime.append(clock_startOverTime);

				

				//获得tmpC位置

				clock_offsetTop=tmpC.position().top;

				clock_divLong=tmpC.height();

				

				clock_scheduleOnDayTable.css({'top':tmpC.offset().top+'px','left':tmpC.offset().left+'px'});

				

				clock_scheduleOnDayTable.show();

			});

			

			//取消选中的时间

			var tmpClose=$("<div>取消</div>").css({'float':'right','overflow':'hidden'});

			tmpClose.click(function(){

				tmp.remove();

				tmpC.remove();

			});

			tmpC.append(tmpClose);

			

			

			tmp.resizable({maxWidth:clock_timeLine.width(),minWidth:clock_timeLine.width(),resize: function(event, ui){

				var Y=tmp.position().top;

				

				//侦测是否与别的方框重贴

				var checkInTmp=0;

				tmpC.prevAll().each(function(){

					var y=$(this).position().top;

					var yl=$(this).height()+y;

					var YL=Y+tmpC.height();

					if((Y>y)&&(Y<yl)||(YL>y)&&(YL<yl)||(y>=Y)&&(yl<=YL))

					{

						checkInTmp=1;

						return;

					}

				});

				if(checkInTmp==1)

				{

					return;

				}

				

				tmpC.css({'height':tmp.height()+'px'});

				tmpC.empty();

				tmpC.append(clock_getMinAndHour(Y,$(this).parent().height(),tmp.height()));

			}});

			tmp.draggable({drag: function() {

					var Y=tmp.position().top;

					

					//侦测是否与别的方框重贴

					var checkInTmp=0;

					tmpC.prevAll().each(function(){

						var y=$(this).position().top;

						var yl=$(this).height()+y;

						var YL=Y+tmpC.height();

						if((Y>y)&&(Y<yl)||(YL>y)&&(YL<yl)||(y>=Y)&&(yl<=YL))

						{

							checkInTmp=1;

							return;

						}

					});

					if(checkInTmp==1)

					{

						return;

					}

					

					tmpC.css({'top':Y+'px'});

					tmpC.empty();

					tmpC.append(clock_getMinAndHour(Y,$(this).parent().height(),yl));

				}

			});

			tmp.mouseover(function(){

				clock_getArrange_check=1;

			});

			tmp.mouseout(function(){

				clock_getArrange_check=0;

			});

		}

	});

}



/**

 * 第一个参数为到父元素顶的距离

 * 第二个参数为父元素的总长

 * 第三个参数为本元素的总长

 * @returns {String}

 */

var clock_startOverTime="";

function clock_getMinAndHour()

{

	var returnString="";

	var tMins=Math.floor((arguments[0]*24*60)/arguments[1]);

	

	var h=Math.floor(tMins/60);

	var m=Math.floor(tMins%60);

	

	returnString=h+":"+m;

	

	tMins=Math.floor(((arguments[0]+arguments[2])*24*60)/arguments[1]);

	h=Math.floor(tMins/60);

	m=Math.floor(tMins%60);

	

	returnString=returnString+"-"+h+":"+m;

	clock_startOverTime=returnString;

	

	return returnString;

}



//显示主体的位置

function clock_displayClock()

{

	$(this).click(function () {

		

		beSubInMDay=$(this).find('.dataDivInscheduleDayNum').html();

		

		var X=$(this).offset().left;

		var Y=$(this).offset().top;

		

		clock_base.css({'left':(X+20)+'px','top':(Y+25)+'px'});

		$(this).parent().parent().parent().append(clock_base);

		

		clock_displayClockOn();

		

		//设计显示已有的日程

		for(var i=0;i<dataBase_scheduleInMonth.length;i++)

		{

			if(dataBase_scheduleInMonth[i][8]==$(this).find('.dataDivInscheduleDayNum').html())

			{

				var tmpC=$("<div></div>").css({'width':'100%', 'height':dataBase_scheduleInMonth[i][5]+'px','top':dataBase_scheduleInMonth[i][4]+'px', 'background':'grey','overflow':'hidden','position': 'absolute','opacity':'0.5'});

				tmpC.append(dataBase_scheduleInMonth[i][3]);

				tmpC.append(dataBase_scheduleInMonth[i][12]);

				clock_ContentLine.append(tmpC);

			}

		}

	});

}



//显示

function clock_displayClockOn()

{

	dataBase_mutexOn();

	clock_timeLine.find("div").remove();

	clock_ContentLine.empty();

	clock_base.show();

	return;

}



//隐藏

function clock_displayClockOff()

{

	dataBase_mutexOff();

	clock_timeLine.find("div").remove();

	clock_ContentLine.empty();

	clock_base.hide();

	return;

}



//创建确定日程的表单

var clock_scheduleOnDayTable;

var clock_scheduleOnDayTableInsertTime;

var clock_scheduleOnDayTableInsertOrderText;

function clock_createScheduleOnDayTable()

{

	clock_scheduleOnDayTable=$("<div></div>").css({'width':'200px', 'height':'300px', 'background':'black' ,'position': 'absolute','overflow':'hidden','z-index':'33'});

	

	var temp_wel=$("<div class='temp_wel'>你好, 你选择的时间是</div>").css({'width':'100%', 'height':'20px','background':'red'});

	clock_scheduleOnDayTable.append(temp_wel);

	

	//被选择的时间

	clock_scheduleOnDayTableInsertTime=$("<div></div>").css({'width':'200px', 'height':'50px', 'background':'black' ,'overflow':'hidden'});

	clock_scheduleOnDayTable.append(clock_scheduleOnDayTableInsertTime);

	

	//选择事件

	var scheduleOnDayTableInsertOrder=$("<div></div>").css({'width':'200px', 'height':'160px', 'background':'red' ,'overflow':'hidden'});

	

	//可能的事件

	var scheduleOnDayTableInsertOrderName=$("<div></div>").css({'width':'200px', 'height':'20px', 'background':'red' ,'overflow':'hidden'});



	//会友地点

	var scheduleOnDayTableInsertOrderNameFriendSelectDiv=$("<div></div>").css({'width':'50px', 'height':'20px', 'float':'left' ,'overflow':'hidden', 'cursor': 'pointer'});

	var scheduleOnDayTableInsertOrderNameFriendSelect=$("<select name='select'></select>");

	scheduleOnDayTableInsertOrderNameFriendSelect.append($("<option value='0'>会友</option>"))

	$.ajax({

		//async:false,

		type: "GET",

		url: "dataBase/getPattenerShop.php",

		//data:{'type':type,'familytype':familyType,'userid':userid},

		cache: false,

		dataType: "json",

		beforeSend:function(XMLHttpRequest) {},

		success: function(msg){

			$.each(msg, function(i, n){

				var temp=$("<option value='在 "+n[0]+" 会友'>"+n[0]+"</option>");

				temp.append(onClickChangeInText);

				scheduleOnDayTableInsertOrderNameFriendSelect.append(temp);

			});

			return false;

		}

	});

	scheduleOnDayTableInsertOrderNameFriendSelectDiv.append(scheduleOnDayTableInsertOrderNameFriendSelect);

	scheduleOnDayTableInsertOrderName.append(scheduleOnDayTableInsertOrderNameFriendSelectDiv);



	//添加学习事件

	var scheduleOnDayTableInsertOrderNameStudyDiv=$("<div></div>").css({'width':'50px', 'height':'20px', 'float':'left' ,'overflow':'hidden', 'cursor': 'pointer'});

	var scheduleOnDayTableInsertOrderNameStudySelect=$("<select name='select'></select>");

	scheduleOnDayTableInsertOrderNameStudySelect.append($("<option value='0'>学习</option>"));

	scheduleOnDayTableInsertOrderNameStudySelect.append($("<option value='在 图书馆 学习'>图书馆</option>").append(onClickChangeInText));

	scheduleOnDayTableInsertOrderNameStudyDiv.append(scheduleOnDayTableInsertOrderNameStudySelect);

	scheduleOnDayTableInsertOrderName.append(scheduleOnDayTableInsertOrderNameStudyDiv);

	//学习的结束

	scheduleOnDayTableInsertOrder.append(scheduleOnDayTableInsertOrderName);

	//可能的事件结束

	

	//放入文本信息

	var scheduleOnDayTableInsertOrderTextArea=$("<div></div>").css({'width':'200px', 'height':'65px', 'background':'red' ,'overflow':'hidden'});

	clock_scheduleOnDayTableInsertOrderText=$("<textarea></textarea>");

	scheduleOnDayTableInsertOrderTextArea.append(clock_scheduleOnDayTableInsertOrderText);

	scheduleOnDayTableInsertOrder.append(scheduleOnDayTableInsertOrderTextArea);

	//文本信息结束

	

	//设置私人信息,朋友可见,公众可见

	var scheduleOnDayTableInsertOrderSetViewArea=$("<div></div>").css({'width':'200px', 'height':'20px', 'background':'blue' ,'overflow':'hidden'});

	var scheduleOnDayTableInsertOrderSetViewPersonal=$("<div><input type='radio' name='viewSet' value='0' checked='checked'/>个人</div>").css({'float':'left'});

	scheduleOnDayTableInsertOrderSetViewArea.append(scheduleOnDayTableInsertOrderSetViewPersonal);

	

	var scheduleOnDayTableInsertOrderSetViewFriends=$("<div><input type='radio' name='viewSet' value='1'/>朋友</div>").css({'float':'left'});

	scheduleOnDayTableInsertOrderSetViewArea.append(scheduleOnDayTableInsertOrderSetViewFriends);

	

	var scheduleOnDayTableInsertOrderSetViewPublic=$("<div><input type='radio' name='viewSet' value='2'/>全部可见</div>").css({'float':'left'});

	scheduleOnDayTableInsertOrderSetViewArea.append(scheduleOnDayTableInsertOrderSetViewPublic);

	

	scheduleOnDayTableInsertOrder.append(scheduleOnDayTableInsertOrderSetViewArea);

	//设置可见性结束

	

	clock_scheduleOnDayTable.append(scheduleOnDayTableInsertOrder);

	//被选择时间结束

	

	//提交和取消

	var scheduleOnDayTableInsertOrderConfirmArea=$("<div></div>").css({'width':'200px', 'height':'20px', 'background':'grey' ,'overflow':'hidden'});

	var scheduleOnDayTableInsertOrderYes=$("<div>确定</div>").css({'cursor': 'pointer','float':'left'});

	scheduleOnDayTableInsertOrderYes.click(function(){

		$.ajax({

			//async:false,

			type: "POST",

			url: "dataBase/updateSchedule.php",

			data:{'clock_startOverTime':clock_startOverTime,'beSubInMDay':beSubInMDay,'clock_offsetTop':clock_offsetTop,'clock_divLong':clock_divLong,'context':clock_scheduleOnDayTableInsertOrderText.val(),'nian':dataBase_current_year,'yue':dataBase_current_month},

			cache: false,

			dataType: "json",

			beforeSend:function(XMLHttpRequest) {},

			success: function(msg){

				clock_scheduleOnDayTable.hide();

				clock_base.hide();

				//获取新的数据

				mutex=0;

				dataBase_getPersonalMonthSchedule();

				return false;

			}

		});

	});

	scheduleOnDayTableInsertOrderConfirmArea.append(scheduleOnDayTableInsertOrderYes);

	var scheduleOnDayTableInsertOrderNo=$("<div>取消</div>").css({'cursor': 'pointer','float':'left'});

	scheduleOnDayTableInsertOrderNo.click(function(){

		clock_scheduleOnDayTable.hide();

	});

	scheduleOnDayTableInsertOrderConfirmArea.append(scheduleOnDayTableInsertOrderNo);

	clock_scheduleOnDayTable.append(scheduleOnDayTableInsertOrderConfirmArea);

	//提交取消结束

	

	$("#scheduleshowputschedule").append(clock_scheduleOnDayTable);

	clock_scheduleOnDayTable.hide();

	

	return false;

}



//被加入的事件.点击事件.当选择被点击的时候, 将文本框的文字改变.

function onClickChangeInText()

{

	$(this).click(function (){

		var temp=$(this).val();

		clock_scheduleOnDayTableInsertOrderText.val(temp);

	});

}





/**

 * 遍历右侧是否有重复的时间段

 * 重复返回true;

 * 不重复返回false;

 * */

function clock_checkContentLineDivForRepeat(ystart,yover)

{

	var check=false;

	clock_ContentLine.children("div").each(function(i){

		var pos=clock_ContentLine.children("div").eq(i).offset();

		var tmp=clock_ContentLine.children("div").eq(i).height()+pos.top;

		

		

		if((ystart>pos.top)&&(ystart<tmp))

		{

			check=true;

			return;

		}

		if((yover>pos.top)&&(yover<tmp))

		{

			check=true;

			return;

		}

	});

	

	return check;

}







